package com.sc.nft.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.meta.business.client.api.ElevenDimensionsBusinessCardClient;
import com.meta.business.client.req.GetBusinessCardPlaneInformationReq;
import com.meta.business.client.req.ObtainToken;
import com.meta.business.client.resp.*;
import com.meta.component.base.api.CommonResult;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.BusinessCardConfirmVO;
import com.sc.nft.entity.vo.JudgeUserIsActivationBusinessCardVO;
import com.sc.nft.entity.vo.UserBusinessCardOrderPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.BusinessCardConfigService;
import com.sc.nft.service.BusinessCardOrderService;
import com.sc.nft.service.UserBusinessCardService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/userBusinessCard")
@Api(tags = "用户个人名片")
public class BusinessCardController {
    
    private final UserBusinessCardService userBusinessCardService;

    private final BusinessCardConfigService businessCardConfigService;

    private final ElevenDimensionsBusinessCardClient elevenDimensionsBusinessCardClient;
    
    private final BusinessCardOrderService businessCardOrderService;


    @PostMapping(value = "/judgeUserIsActivationBusinessCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "判定用户是否开通名片", notes = "判定用户是否开通名片", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<JudgeUserIsActivationBusinessCardVO> judgeUserIsActivationBusinessCard() {
        return Result.success(userBusinessCardService.judgeUserIsActivationBusinessCard(UserPool.getUser()));
    }

    @PostMapping(value = "/createUserToken", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建用户token（获取token)", notes = "创建用户token（获取token)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result createUserToken() {
        UserInfo user = UserPool.getUser();
        ObtainToken obtainToken = new ObtainToken();
        obtainToken.setOwner(user.getUserUuid());
        CommonResult<GetTokenRes> getTokenResCommonResult = elevenDimensionsBusinessCardClient.getBusinessCardToken(obtainToken);
        if(getTokenResCommonResult.getCode() != 200){
            return Result.fail(getTokenResCommonResult.getMessage());
        }
        return Result.success(getTokenResCommonResult.getData().getData());
    }


    @PostMapping(value = "/getUserBusinessCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户名片地址", notes = "获取用户名片地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetUserBusinessCardRes> getUserBusinessCard() {
        UserInfo user = UserPool.getUser();
        GetUserBusinessCardReq obtainToken = new GetUserBusinessCardReq();
        obtainToken.setOwner(user.getUserUuid());
        CommonResult<GetUserBusinessCardRes> getTokenResCommonResult = elevenDimensionsBusinessCardClient.getUserBusinessCard(obtainToken);
        if (getTokenResCommonResult.getCode()!=200) {
            return Result.fail(getTokenResCommonResult.getMessage());
        }
        return Result.success(getTokenResCommonResult.getData());
    }



    @PostMapping(value = "/webSkip", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "web跳转地址获取", notes = "web跳转地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result webSkip(@RequestBody WebSkipReq webSkipReq) {
        UserInfo user = UserPool.getUser();
        webSkipReq.setOwner(user.getUserUuid());
        CommonResult commonResult = elevenDimensionsBusinessCardClient.webSkip(webSkipReq);
        if (commonResult.getCode()!=200L) {
            throw new GlobalRunTimeException(commonResult.getMessage());
        }
        return Result.success(commonResult.getData());
    }

    @PostMapping(value = "/confirm", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建名片确认", notes = "创建名片确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<BusinessCardConfirmVO> businessCardConfirm() {
        UserInfo user = UserPool.getUser();
        BusinessCardConfirmVO businessCardConfirmVO = businessCardConfigService.businessCardConfirm(user.getId());
        return Result.success(businessCardConfirmVO);
    }

    @PostMapping(value = "/obtainPlaneInformation", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取名片平面信息", notes = "获取名片平面信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetBusinessCardPlaneInformationDataRes> obtainPlaneInformation() {
        UserInfo user = UserPool.getUser();
        GetBusinessCardPlaneInformationReq getBusinessCardPlaneInformationReq = new GetBusinessCardPlaneInformationReq();
        getBusinessCardPlaneInformationReq.setOwner(user.getUserUuid());
        CommonResult<GetBusinessCardPlaneInformationRes> commonResult = elevenDimensionsBusinessCardClient.obtainPlaneInformation(getBusinessCardPlaneInformationReq);
        log.info("获取平面信息:{}", JSON.toJSON(commonResult));
        if (commonResult.getCode()!=200L) {
            throw new GlobalRunTimeException(commonResult.getMessage());
        }else {
            if(ObjectUtil.isNotNull(commonResult.getData()) && ObjectUtil.isNotNull(commonResult.getData().getData())){
                Result.success().setMessage(commonResult.getMessage());
                return Result.success(commonResult.getData().getData());
            }
        }
        throw new GlobalRunTimeException(commonResult.getMessage());
    }

    @PostMapping(value = "/userBusinessCardOrderPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户订单列表", notes = "用户订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<UserBusinessCardOrderPageVO> userBusinessCardOrderPage(@RequestBody PageRequest request) {
        UserInfo user = UserPool.getUser();
        Assert.isTrue(ObjectUtil.isNotNull(user), () -> new GlobalRunTimeException("用户不存在"));
        return PageResult.success(businessCardOrderService.userBusinessCardOrderPage(request.toMp(), user.getId()));
    }


}
